package com.cskaoyan.market.service.admin;

import com.cskaoyan.market.annotation.Log;
import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.db.domain.MarketAdminExample;
import com.cskaoyan.market.db.mapper.MarketAdminMapper;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2024/5/4 9:26
 * @Version 1.0
 */

@Service
public class MarketAdminServiceImpl implements MarketAdminService {
    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Override
    public List<MarketAdmin> list(Integer limit, Integer page, String username, String sort, String order) {
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        marketAdminExample.setOrderByClause(sort + " " + order);
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        PageHelper.startPage(page, limit);
        List<MarketAdmin> marketAdminList = marketAdminMapper.selectByExampleSelective(marketAdminExample, MarketAdmin.Column.id, MarketAdmin.Column.username, MarketAdmin.Column.avatar, MarketAdmin.Column.roleIds);

        return marketAdminList;
    }

    @Override
    @Log(type = 1,action = "添加管理员")
    public MarketAdmin create(MarketAdmin marketAdmin) {
        try {
            marketAdminMapper.insertSelective(marketAdmin);
            return marketAdmin;
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Boolean update(MarketAdmin marketAdmin) {
        try {
            marketAdminMapper.updateByPrimaryKeySelective(marketAdmin);
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Log(type = 1,action = "修改密码")
    @Override
    public MarketAdmin updatePassword(@Valid MarketAdmin marketAdmin1, String oldPassword,BindingResult bindingResult) {
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        //criteria用来构造一系列的where条件
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();

        criteria.andIdEqualTo(marketAdmin1.getId());

            MarketAdmin marketAdmin = marketAdminMapper.selectOneByExample(marketAdminExample);
            if(Objects.equals(marketAdmin.getPassword(), oldPassword)) {
                marketAdminMapper.updateByExample(marketAdmin1, marketAdminExample);
                return marketAdmin1;
            }else {
                return null;
            }

        }
 @Log(type = 1,action = "删除管理员")
    @Override
    public MarketAdmin delete(MarketAdmin marketAdmin) {
        try {
            marketAdminMapper.deleteByPrimaryKey(marketAdmin.getId());
            return marketAdmin;
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //登录
    @Override
    public MarketAdmin getByUsername(String username) {
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        marketAdminExample.createCriteria().andUsernameEqualTo(username);
        return marketAdminMapper.selectOneByExample(marketAdminExample);
    }

    @Log(type = 1,action = "登录")
    @Override
    public MarketAdmin login(MarketAdmin marketAdmin) {
        if(marketAdmin!=null){
            return marketAdmin;
        }else return null;
    }

    @Log(type = 1,action = "退出登录")
    @Override
    public MarketAdmin logout(Session session) {
        if(session!=null){
            return (MarketAdmin) session.getAttribute("admin");
        }else {
            return null;
        }
    }

    @Override
    public MarketAdmin selectById(String roleId) {
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        marketAdminExample.createCriteria().andIdEqualTo(Integer.valueOf(roleId));
        return marketAdminMapper.selectOneByExample(marketAdminExample);
    }
}
